﻿using System;
using System.Web;
using System.Web.Mvc;
using Sharp.DataTransferObject.CMS;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Infrastructure.Search;
using Sharp.Platform;
using Sharp.Platform.Mvc;
using Sharp.Platform.Owin;
using Sharp.ServiceContracts.CMS;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class VIUController : Controller
    {
        // GET: VIU
        public ActionResult Index()
        {
            ViewBag.Todo = Request.QueryString["todo"];
            return View();
        }
        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            var model = new VIUViewModel();
            model.TempFlag = Guid.NewGuid().ToString();
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                model.Colmuns = proxy.Channel.GetColumns().Data;
                model.Fields = proxy.Channel.GetFieldList(true).Data;
                model.Categories = proxy.Channel.GetCategoryList(true).Data;

                return View("Edit", model);
            }
        }


        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            var model = new VIUViewModel();
            model.Id = id;
            model.TempFlag = Guid.NewGuid().ToString();
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                model.Colmuns = proxy.Channel.GetColumns().Data;
                model.Fields = proxy.Channel.GetFieldList(true).Data;
                model.Categories = proxy.Channel.GetCategoryList().Data;
                //model.PropertiesWithTags = proxy.Channel.GetPropertiesWithTags().Data;

                return View("Edit", model);
            }
        }
        public ActionResult GetInfo(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.GetVIU(id);

                return this.JsonNet(res);
            }
        }


        [HttpPost]
        public ActionResult ReceivePic(string tempFlag)
        {
            if (tempFlag.IsNullOrEmpty())
            {
                return Json(new Result()
                {
                    Err = nameof(tempFlag) + "参数缺少。"
                });
            }
            var files = Request.Files;
            if (files.Count > 0)
            {
                try
                {
                    var dir = new System.IO.DirectoryInfo(Server.MapPath("/Upload/temp/" + tempFlag));
                    if (!dir.Exists)
                    {
                        dir.Create();
                    }

                    HttpPostedFileBase file = files[0];
                    string filePath = "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + file.FileName;
                    string fullPath = dir.FullName + filePath;
                    file.SaveAs(fullPath);
                    return Json(new Result()
                    {
                        Data = "/Upload/temp/" + tempFlag + filePath.Replace("\\", "/")
                    });

                }
                catch
                {
                    return Json(new Result() { Err = "服务器内部错误。" });
                }
            }
            return Json(new Result() { Err = "上传为空。" });
        }

        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param, string todo)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var pf = param.ToPagingFeature();
                if (todo == "my")
                {
                    pf.Filters.Add(new SearchCondition("EditorId", "EditorId", SearchType.Equal, new[] { SharpUserManager.Current.Id.ToString() }));
                }
                else if (todo == "review")
                {
                    pf.Filters.Add(new SearchCondition("IsChecked", "IsChecked", SearchType.Equal, new[] { "False" }));
                }

                var page = proxy.Channel.GetVIUPaged(pf);

                return this.Store(page);
            }
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Save(VIUDTO vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.SaveVIU(vm);

                return this.JsonNet(res);
            }
        }

        public ActionResult Review(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var data = proxy.Channel.GetVIU(id).Data;
                var model = data.To<VIUReViewViewModel>();

                return View("Review", model);
            }
        }

        [HttpPost]
        public ActionResult SaveReview(ApproveHistoryDTO vm)
        {
            if (vm == null)
            {
                return Json(new Result() { Err = "提交数据出错。" });
            }
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.CheckInVIU(vm.VIUId, vm.IsChecked, vm.Remark);

                return Json(res);
            }
        }
    }
}